package ldh.fx.adminfx.commpont;

import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXPasswordField;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import ldh.fx.adminfx.fxanimations.FadeOutTransition;
import ldh.fx.adminfx.help.MovableHelper;
import ldh.fx.adminfx.util.RegionUtil;
import ldh.fx.adminfx.util.StageUtil;

import java.net.URL;
import java.util.ResourceBundle;
import java.util.function.Consumer;

public class LoginPane extends AnchorPane implements Initializable {

    @FXML public JFXButton closeBtn;
    @FXML public TextField userName;
    @FXML public PasswordField passwordField;
    @FXML public Pane contentPane;
    @FXML public Pane loginGroup;
//    @FXML public Pane shadowPane;

    private Consumer<?> consumer = null;

    public LoginPane() {
        RegionUtil.loadFxml(this, this,"/fxml/LoginPage.fxml");

        MovableHelper.movable(contentPane);

        loginGroup.getChildren().add(new LoginCircle(400, 390));
    }

    public Scene createScene() {
        Scene scene = new Scene(this, 800, 400);
        scene.setFill(Color.TRANSPARENT);
        scene.getStylesheets().add(this.getClass().getResource("/css/skin/base.css").toExternalForm());
        scene.getStylesheets().add(this.getClass().getResource("/css/login.css").toExternalForm());
        scene.getStylesheets().add(this.getClass().getResource("/css/ui.css").toExternalForm());
//        scene.getStylesheets().add("org/kordamp/bootstrapfx/bootstrapfx.css");
        scene.getStylesheets().add(this.getClass().getResource("/css/bootstrapfx.css").toExternalForm());
        return scene;
    }

    @FXML
    public void close() {
        close(null);
    }

    @FXML
    public void loginBtn() {
        close((Void)->{
            if (consumer != null) {
                consumer.accept(null);
            }
        });
    }

    public void setConsumer(Consumer<?> consumer) {
        this.consumer = consumer;
    }

    private void close(Consumer<?> consumer) {
        FadeOutTransition fadeOutTransition = new FadeOutTransition(this);
        fadeOutTransition.setOnFinished(e->{
            StageUtil.getStage().close();
            if (consumer != null) {
                consumer.accept(null);
            }
        });
        fadeOutTransition.playFromStart();
    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        Rectangle clip = new Rectangle();
        clip.widthProperty().bind(this.widthProperty());
        clip.heightProperty().bind(this.heightProperty());
        clip.setArcWidth(15);
        clip.setArcHeight(15);
        this.setClip(clip);
    }
}
